<?php
if (count($rows) > 0) {
	$hidden = false;//loading from merchant account
	$anonymous_lbl = BUtils::T('anonymous_lbl');
	$points_s_lbl = BUtils::T('points_s_lbl');
	$visits_lbl = BUtils::T('visits_lbl');
?>
<table class="table table-striped table-hover table-customer">
	<tbody>
<?php
foreach ($rows as $r) {
	$visit_timeago = Yii::app()->format->timeago((int)$r['ts_last_time']);
	$visit_time = Yii::app()->locale->dateFormatter->formatDateTime($r['ts_last_time'], null, 'short');
	$visit_info = BUtils::T('last_visited_lbl', array('{time_ago}'=>$visit_timeago))  . ' ('.$visit_time.')';
	if (empty($r['id_user'])) {
		$cust_name = $anonymous_lbl;
		$cust_url = Yii::app()->baseUrl . '/img/user-128.png';
		$cust_contact = '';
	} else {
		//set user name
		if (!empty($r['name'])) $cust_name = substr($r['name'], 0, strpos($r['name'], ' '));
		else $cust_name = $anonymous_lbl;
		
		//set active phone number/email
		$cust_contact = '<ul class="icons-ul">';
		if (!empty($r['mobile'])) {
			$cust_contact .= '<li><i class="icon-li icon-phone"></i> ' . CustomerHelper::getMobile($r['mobile'], $hidden) . '</li>';
		}
		if(!empty($r['email'])) {
			$cust_contact .= '<li><i class="icon-li icon-envelope-alt"></i> ' . CustomerHelper::getEmail($r['email'], $hidden) . '</li>';
		}
		$cust_contact .= '</ul>';
		
		//set user picture
		if (empty($r['picture'])) $cust_url = Yii::app()->baseUrl . '/img/user-128.png';
		else $cust_url = $r['picture'];
	}
	
	//amount + points + redeemed points
	$amount = Yii::app()->locale->numberFormatter->formatDecimal($r['amount']);
	$points = Yii::app()->locale->numberFormatter->formatDecimal($r['point']);
	$redeemed_points = Yii::app()->locale->numberFormatter->formatDecimal((float)$r['point'] - (float)$r['amount']);
	
	//followed tag
	$followed_span = '';
	if (!empty($r['followed'])) {
		$followed_span = '<span class="label label-success arrowed-in-right">'.BUtils::T('follower_lbl').'</span>';
	}
	
	//notes
	$url_notes = Yii::app()->createUrl('/customer/notes', array('uid'=>$r['id_user']));
	if (empty($r['notes'])) $icon_notes = '<a class="gray popover-right" href="javascript:;" data-poload="'.$url_notes.'" id="popover-icon-'.$r['id_user'].'"><i class="icon-file-alt bigger-130" id="icon-'.$r['id_user'].'"></i></a>';
	else $icon_notes = '<a class="blue popover-right" href="javascript:;" data-poload="'.$url_notes.'" id="popover-icon-'.$r['id_user'].'"><i class="icon-file-text-alt bigger-130" id="icon-'.$r['id_user'].'"></i></a>';
?>
<tr>
	<td class="user">
		<img src="<?php echo $cust_url; ?>" alt="" onerror="this.onerror=null;this.src='<?php echo Yii::app()->baseUrl; ?>/img/user-128.png';" />
	</td>
	<td class="uinfo">
		<div class="cust-name">
			<span class="blue"><?php echo $cust_name; ?></span>
		</div>
		<div class="cust-contact"><?php echo $cust_contact; ?></div>
		<div class="visit-time"><?php echo $visit_info; ?></div>
	</td>
	<td class="uvisits">
		<div class="center text-big blue">
			<?php echo $r['visit_times']; ?>
		</div>
		<div class="center"><?php echo $visits_lbl; ?></div>
	</td>
	<td class="upoints">
		<div class="center text-big blue">
			<?php echo $points; ?>
		</div>
		<div class="center"><?php echo $points_s_lbl; ?></div>
	</td>
</tr>
<?php } ?>
	</tbody>
</table>

<?php
//pagination
$total_n_customers = BUtils::T('total_n_customers_lbl', array('{num}'=>$pages->getItemCount()));
echo '<div role="grid" class="dataTables_wrapper">
	<div class="row-fluid">
	<div class="span6"><div class="dataTables_info">'.$total_n_customers.'</div></div>
	<div class="span6">
	<div class="dataTables_paginate paging_bootstrap pagination">';
$this->widget('CLinkPager', array(
	'currentPage' => $pages->getCurrentPage(),
	'itemCount' => $pages->getItemCount(),
	'pageSize' => $pages->getPageSize(),
	'prevPageLabel' => '&lt;',
	'nextPageLabel' => '&gt;',
	'firstPageLabel' => '&lt;&lt;',
	'lastPageLabel' => '&gt;&gt;',
	'maxButtonCount' => 5,
	'header' => '',
	'selectedPageCssClass' => 'active',
	'htmlOptions' => array('class'=>'page-ul'),
));
echo '</div></div></div></div>';
?>
			
<?php } else { ?>
	<div class="well well-large gray">
		<h3 class=""><?php echo BUtils::T('customers_no_title_lbl'); ?></h3>
		<p><?php echo BUtils::T('customers_not_found_eg'); ?></p>
	</div>
<?php } ?>
<script type="text/javascript">
$(function() {
	//pagination
	$('.page-ul a').click(function(){
		$.ajax({
			'beforeSend':beforeSend, 'success':success,
			'type':'POST', 'url':$(this).attr('href'), 'data':$('#search_form').serialize()
		});
		return false;
	});
	//show notes popover
	$('.popover-right').click(function(ev) {
		ev.preventDefault();
		ev.stopPropagation();
		var e = $(this);
		e.unbind('click');
		$.get(e.data('poload'), function(d) {
			e.popover({title:'Notes', content: d, placement:'right', html:true}).popover('show');
		});
	});
});
</script>